var selects = document.getElementsByTagName('select');

var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;

function $$$(id) {
	return document.getElementById(id);
}

function stopBubbling (ev) {	
	ev.stopPropagation();
}

/**
 * 生成动态下拉框
 */
function refreshSelect() {
	for (var i=0;i<selects.length;i++){
		selects[i].style.display = 'none';
		var select_tag = document.createElement('div');
		if ($("#select_" + selects[i].id) != null) {
			$("#select_" + selects[i].id).remove();
		}
		select_tag.id = 'select_' + selects[i].id;
		select_tag.className = 'select_box';
		selects[i].parentNode.insertBefore(select_tag,selects[i]);

		var select_info = document.createElement('div');	
		select_info.id = 'select_info_' + selects[i].id;
		select_info.className='tag_select';
		select_info.style.cursor='pointer';
		select_tag.appendChild(select_info);

		var select_ul = document.createElement('ul');	
		select_ul.id = 'options_' + selects[i].id;
		select_ul.className = 'tag_options';
		select_ul.style.position='absolute';
		select_ul.style.display='none';
		select_ul.style.zIndex='999';
		select_tag.appendChild(select_ul);

		rOptions(i,selects[i].id);
		
		mouseSelects(selects[i].id);

		if (isIE){
			selects[i].onclick = new Function("clickLabels3('"+selects[i].id+"');window.event.cancelBubble = true;");
		} else if(!isIE){
			selects[i].onclick = new Function("clickLabels3('"+selects[i].id+"')");
			selects[i].addEventListener("click", stopBubbling, false);
		}	
	 }
}


function rOptions(i, id) {
	var options_ul = 'options_' + id;
	for (var n=0;n<selects[i].options.length;n++){	
		option_li = document.createElement('li');
		option_li.style.cursor='pointer';
		option_li.className='open';
		$$$(options_ul).appendChild(option_li);

		option_text = document.createTextNode(selects[i].options[n].text);
		option_li.appendChild(option_text);

		option_selected = selects[i].options[n].selected;

		if(option_selected){
			option_li.className='open_selected';
			option_li.id='selected_' + id;
			$$$('select_info_' + id).appendChild(document.createTextNode(option_li.innerHTML));
		}
		
		option_li.onmouseover = function(){	this.className='open_hover';};
		option_li.onmouseout = function(){
			if (this.id=='selected_' + id) {
				this.className='open_selected';
			} else {
				this.className='open';
			}
		};
	
		option_li.onclick = new Function("clickOptions("+i+","+n+",'"+selects[i].id+"')");
	}
}

function mouseSelects(id){
	var sincn = 'select_info_' + id;

	$$$(sincn).onmouseover = function(){
		if(this.className=='tag_select') this.className='tag_select_hover'; 
	};
	$$$(sincn).onmouseout = function(){
		if(this.className=='tag_select_hover') this.className='tag_select'; 
	};

	if (isIE){
		$$$(sincn).onclick = new Function("clickSelects('"+id+"');window.event.cancelBubble = true;");
	}
	else if(!isIE){
		$$$(sincn).onclick = new Function("clickSelects('"+id+"');");
		$$$('select_info_' +id).addEventListener("click", stopBubbling, false);
	}

}

function clickSelects(id){
	var sincn = 'select_info_' + id;
	var sinul = 'options_' + id;	

	for (var i=0;i<selects.length;i++){	
		if(selects[i].id == id){				
			if( $$$(sincn).className =='tag_select_hover'){
				$$$(sincn).className ='tag_select_open';
				$$$(sinul).style.display = '';
			}
			else if( $$$(sincn).className =='tag_select_open'){
				$$$(sincn).className = 'tag_select_hover';
				$$$(sinul).style.display = 'none';
			}
		}
		else{
			$$$('select_info_' + selects[i].id).className = 'tag_select';
			$$$('options_' + selects[i].id).style.display = 'none';
		}
	}

}

function clickOptions(i, n, id){  
	var li = $$$('options_' + id).getElementsByTagName('li');
	//获取上一次选中值
	var x1=selects[i].id;
	var perV=$("#"+x1+"  option:selected").val();

	if ($$$('selected_' + id) != null) {
		$$$('selected_' + id).className='open';
		$$$('selected_' + id).id='';
	}
	li[n].id='selected_' + id;
	li[n].className='open_hover';
	$$$('select_' + id).removeChild($$$('select_info_' + id));

	select_info = document.createElement('div');
	select_info.id = 'select_info_' + id;
	select_info.className='tag_select';
	select_info.style.cursor='pointer';
	$$$('options_' + id).parentNode.insertBefore(select_info,$$$('options_' + id));

	mouseSelects(id);
	if ($$$('selected_' + id) != null) {
		$$$('select_info_' + id).appendChild(document.createTextNode(li[n].innerHTML));
		$$$( 'options_' + id ).style.display = 'none' ;
		$$$( 'select_info_' + id ).className = 'tag_select';
	}

	selects[i].options[n].selected = 'selected';
	//获取当前选中值
	var x=selects[i].id;
	var curV=$("#"+x+"  option:selected").val();

	if  (curV != perV) {//下拉框值做了改变，方可触发事件
		eval($("#"+x).attr("onchange"));
	}
}

window.onload = function(e) {
	refreshSelect();
	document.onclick = function(){
		for (var i = 0; i < selects.length; i++) {
			selects = document.getElementsByTagName('select');
			if ($$$('select_info_' + selects[i].id) != null) {
				$$$('select_info_' + selects[i].id).className = 'tag_select';
				$$$('options_' + selects[i].id).style.display = 'none';
			}
		}
	};
};






